package org.ftc.web;

import java.io.File;
import java.io.FileInputStream;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.ftc.form.FtcLogForm;
import org.ftc.service.FtcLogService;
import org.ftc.service.FtcLogService.Log;
import org.ftc.util.Global;
import org.ftc.util.ListCalculator;
import org.ftc.util.TxtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.FileCopyUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
public class FtcLogController {
	
	@Autowired
	private FtcLogService ftcLogService;
	
	@RequestMapping(value = "/ftcLog.htm")
    public String narlLog( ModelMap model ) {
		model.addAttribute("searchForm", new FtcLogForm() );//
		model.addAttribute("logAction", Log.values() );//
		return "ftcLog/ftcLog";
    }
	
	@RequestMapping(value = "/ftcLog/list.json" , method=RequestMethod.POST)
	public @ResponseBody Map<String, Object> list(FtcLogForm form , BindingResult binding, ModelMap model ) {
		Map<String, Object> json = new HashMap<String, Object>();
		ListCalculator listCal = new ListCalculator(); 
		int pageSize = form.getRows();
		listCal.setRecordsPerPage( pageSize );
		Integer totalRecs = (int)ftcLogService.count(form);
		int totalPages = listCal.totalPages(totalRecs);  
		int pageNo = form.getPage();
		pageNo = (pageNo >= totalPages) ? totalPages : pageNo;
		int firstRes = listCal.firstResult(pageNo);  
		json.put("total", totalRecs.toString() ); 
		json.put("rows", ftcLogService.findPage(form, firstRes, pageSize));
		return json; 
    }
	
	@RequestMapping(value = "/ftcLog/exp.htm", method=RequestMethod.POST)
    public void exp(FtcLogForm form , BindingResult binding, ModelMap model,
    		 HttpServletRequest request, HttpServletResponse response) {
		try { 
			ftcLogService.before();
			File dest = TxtUtil.writeCsv( ftcLogService.findPageMap(form) );
			String name = URLEncoder.encode("日誌.txt", "UTF-8");  
			response.setHeader("content-disposition", "attachment; filename=" + name );
			FileCopyUtils.copy(new FileInputStream( dest ), response.getOutputStream());
			ftcLogService.save(Log.EXP, this.getClass().toString() );
			response.flushBuffer(); 
		} catch (Exception e) {
			e.printStackTrace(); 
		}
		
    }
	
	@InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(Global.APP.DATE_FORMAT); 
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
    }	
}
